package net.quanter.demo.junit5.mybatis.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import net.quanter.demo.junit5.mybatis.dao.entity.User;
import net.quanter.demo.junit5.mybatis.dao.mapper.UserMapper;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/***
 * 基于mybatis+spring的controller
 * 目的是做单元测试
 * 如果本地启动，需要启动mysql，修改application.yml确保能连接上mysql
 * 2020-06-04
 * @author 王老实
 *
 */
@RestController
@RequestMapping("/mysql")
public class CrudController {

    @Resource
    private UserMapper userMapper;

    @PostMapping("/insert")
    public int insert(
            @RequestParam("username") String username,
            @RequestParam("age") Integer age) {
        User user = User.builder().username(username).age(age).build();
        return userMapper.insert(user);
    }

    @PutMapping("/update/{id}")
    public boolean update(
            @PathVariable("id") Integer id,
            @RequestParam("username") String username,
            @RequestParam("age") Integer age) {
        User user = userMapper.selectById(id);
        if (user != null) {
            user.setUsername(username);
            user.setAge(age);
            int count = userMapper.updateById(user);
            return count > 0;
        } else {
            return false;
        }
    }

    @DeleteMapping("/delete/{id}")
    public int delete(@PathVariable("id") Integer id) {
        return userMapper.deleteById(id);
    }

    @GetMapping("/select/{id}")
    public User select(@PathVariable("id") Integer id) {
        return userMapper.selectById(id);
    }

    @GetMapping("/select")
    public List<User> selectByName(@RequestParam("username") String username) {
        User query = new User();
        query.setUsername(username);
        QueryWrapper<User> queryWrapper = new QueryWrapper();
        queryWrapper.setEntity(query);
        List<User> list = userMapper.selectList(queryWrapper);
        return list;
    }

    @GetMapping("/countByUserName")
    public Integer countByUserName(
            @RequestParam("username") String username){
        return userMapper.countByUserName(username);
    }

    @GetMapping("/selectByPage")
    public IPage<User> selectByPage(
            @RequestParam(value="size",defaultValue = "2") long size,
            @RequestParam(value="current",defaultValue = "0") long current,
            @RequestParam(value="age") Integer age){
        Page<User> page = new Page<>();
        page.setSize(size);
        page.setCurrent(current);
        List<OrderItem> orders = new ArrayList<>();
        orders.add(OrderItem.desc("age"));
        page.setOrders(orders);
        IPage<User> userResult = userMapper.selectByPage(page,age);
        return userResult;
    }
}
